import { Controller, Get, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';
import {
  FeishuService,
  TableRecordList,
  TableFieldList,
  TableViewList,
} from './feishu.service';

@ApiTags('feishu')
@Controller('feishu')
export class FeishuController {
  constructor(private readonly feishuService: FeishuService) {}

  @Get('table-data')
  @ApiOperation({ summary: '获取飞书多维表格数据' })
  @ApiResponse({ status: 200, description: '返回多维表格数据' })
  @ApiQuery({ name: 'viewId', required: false, description: '视图ID' })
  @ApiQuery({
    name: 'pageSize',
    required: false,
    description: '每页记录数',
    type: Number,
  })
  @ApiQuery({ name: 'pageToken', required: false, description: '分页标记' })
  async getTableData(
    @Query('viewId') viewId?: string,
    @Query('pageSize') pageSize?: number,
    @Query('pageToken') pageToken?: string,
  ): Promise<{
    success: boolean;
    data: TableRecordList | null;
    message: string;
  }> {
    return this.feishuService.getTableRecords(
      viewId,
      pageSize ? parseInt(pageSize as any, 10) : 100,
      pageToken,
    );
  }

  @Get('table-metadata')
  @ApiOperation({ summary: '获取飞书多维表格字段信息' })
  @ApiResponse({ status: 200, description: '返回多维表格字段信息' })
  async getTableMetadata(): Promise<{
    success: boolean;
    data: TableFieldList | null;
    message: string;
  }> {
    return this.feishuService.getTableMetadata();
  }

  @Get('table-views')
  @ApiOperation({ summary: '获取飞书多维表格视图列表' })
  @ApiResponse({ status: 200, description: '返回多维表格视图列表' })
  async getTableViews(): Promise<{
    success: boolean;
    data: TableViewList | null;
    message: string;
  }> {
    return this.feishuService.getTableViews();
  }
}
